package cn.qyd.aqs;

/**
 * @author 邱运铎
 * @date 2024-03-19 0:44
 */
public class MyDemo {
    private volatile int count = 0;
    private volatile boolean isLocked = true;

    public void task1() {
        while (isLocked) {
            isLocked = false;
            count++;
            System.out.println("task1 : " + count);
            if (count == 20)
                break;
        }
    }

    public void task2() {
        while (!isLocked) {
            isLocked = true;
            count++;
            System.out.println("task2 : " + count);
            if (count == 20)
                break;
        }
    }
    public static void main(String[] args) {
        MyDemo demo = new MyDemo();

        new Thread(() -> demo.task1()).start();
        new Thread(() -> demo.task2()).start();

        while (Thread.activeCount() > 2)
            Thread.yield();
    }
}
